package xdj.android.personal.namespace.Views;

import java.util.ArrayList;
import java.util.HashMap;

import xdj.android.personal.R;
import xdj.android.personal.namespace.Business.IdPosDBAccess;
import xdj.android.personal.namespace.Entity.IdPosEntity;
import xdj.android.personal.namespace.utils.ConstUtil;
import xdj.android.personal.namespace.utils.MessageDilaogUtil;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;

public class ArrivedIdPosActivity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.arrivedidposview);
		Bundle bundle = this.getIntent().getExtras();
		ArrayList<HashMap<String, Object>> data = (ArrayList<HashMap<String, Object>>) bundle
				.getSerializable(ConstUtil.IDPOS_STRING);
		sAdapter = new MyAdapter(this, data,
				R.layout.arrived_idpos_checked_view, new String[]
				{ ConstUtil.IDPOS_ID, ConstUtil.IDPOS_NAME,
						ConstUtil.IDPOS_CHECKED }, new int[]
				{ R.id.arrived_idpos_checked_txtid,
						R.id.arrived_idpos_checked_txtname,
						R.id.arrived_idpos_view_checkbox });
		lvListView = (ListView) findViewById(R.id.arrived_Idpos_View_lv);
		lvListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		lvListView.setOnItemClickListener(new OnItemClickListener()
		{

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				Intent intent = new Intent(ArrivedIdPosActivity.this,
						idposdetailsActivity.class);

				startActivity(intent);
			}

		});
		lvListView.setAdapter(sAdapter);
		btnArrive = (Button) findViewById(R.id.arrived_idpos_view_btnArrived);
		btnArrive.setOnClickListener(new View.OnClickListener()
		{

			public void onClick(View v)
			{
				ArrayList<IdPosEntity> list = sAdapter.getSelectItems();
				if (list.size() == 0)
				{
					MessageDilaogUtil.showDialog(ArrivedIdPosActivity.this,
							ConstUtil.IDPOS_SELECT_NOTHING_STRING);
					return;
				}
				IdPosDBAccess dbAccess = new IdPosDBAccess(
						ArrivedIdPosActivity.this);
				int count = dbAccess.Insert(list);

			}
		});
	}

	private ListView lvListView;
	private Button btnArrive;
	private MyAdapter sAdapter;

	public class MyAdapter extends BaseAdapter
	{
		ArrayList<HashMap<String, Object>> datArrayList;
		Context _c;
		LayoutInflater m_inInflater;
		String[] keys;
		int[] values;
		int _resource;
		HashMap<String, Object> map;

		public MyAdapter(Context c, ArrayList<HashMap<String, Object>> data,
				int resource, String[] from, int[] to)
		{
			_c = c;
			datArrayList = data;
			_resource = resource;
			m_inInflater = LayoutInflater.from(c);
			keys = new String[from.length];
			values = new int[to.length];
			System.arraycopy(from, 0, keys, 0, from.length);
			System.arraycopy(to, 0, values, 0, to.length);

		}

		public int getCount()
		{
			return datArrayList.size();
		}

		public Object getItem(int position)
		{
			return datArrayList.get(position);
		}

		public long getItemId(int position)
		{
			return position;
		}

		public ArrayList<IdPosEntity> getSelectItems()
		{
			ArrayList<IdPosEntity> list = new ArrayList<IdPosEntity>();
			for (HashMap<String, Object> hash : datArrayList)
			{
				boolean checked = (Boolean) hash.get(ConstUtil.IDPOS_CHECKED);
				if (checked)
				{
					IdPosEntity entity = new IdPosEntity();
					entity.ID = hash.get(ConstUtil.IDPOS_ID).toString();
					entity.Name = hash.get(ConstUtil.IDPOS_NAME).toString();
					list.add(entity);
				}
			}
			return list;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			HolderView hView = null;
			if (convertView == null)
			{
				convertView = m_inInflater.inflate(_resource, null);
				hView = new HolderView();
				hView.txtIdPosId = (TextView) convertView
						.findViewById(values[0]);
				hView.txtIdPosName = (TextView) convertView
						.findViewById(values[1]);
				hView.check = (CheckBox) convertView.findViewById(values[2]);
				convertView.setTag(hView);
			} else
			{
				hView = (HolderView) convertView.getTag();
			}
			map = datArrayList.get(position);
			hView.txtIdPosId.setText(String.valueOf(map.get(keys[0])));
			hView.txtIdPosName.setText(String.valueOf(map.get(keys[1])));
			hView.txtIdPosName.setOnClickListener(new View.OnClickListener()
			{

				public void onClick(View v)
				{
					Intent intent = new Intent(ArrivedIdPosActivity.this,
							idposdetailsActivity.class);
					startActivity(intent);

				}
			});
			hView.check
					.setOnCheckedChangeListener(new OnCheckedChangeListener()
					{

						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked)
						{
							// TODO Auto-generated method stub
							if (isChecked)
							{
								map.put(keys[2], true);
							} else
							{
								map.put(keys[2], false);
							}
						}
					});
			hView.check
					.setChecked(Boolean.valueOf(map.get(keys[2]).toString()));
			return convertView;
		}

		private class HolderView
		{
			TextView txtIdPosId;
			TextView txtIdPosName;
			CheckBox check;
		}

	}
}
